package com.yunyi.user.controller;

import com.yunyi.model.worker.WorkerInfo;
import com.yunyi.model.worker.dto.WorkOrderInfo;
import com.yunyi.model.worker.dto.WorkTimeDto;
import com.yunyi.response.CommonCode;
import com.yunyi.response.QueryResponseResult;
import com.yunyi.response.QueryResult;
import com.yunyi.response.ResponseResult;
import com.yunyi.user.constant.Constant;
import com.yunyi.user.response.UserCode;
import com.yunyi.user.service.WorkerService;
import com.yunyi.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName WorkerController
 * @Description 员工相关接口信息
 * @Author leizhang
 * Date 2021/8/17 2:39 下午
 * @Version 1.0
 **/
@RestController
@RequestMapping("/workerInfoApi")
public class WorkerController {
    private static Logger LOGGER = LoggerFactory.getLogger(WorkerController.class);
    @Autowired
    private WorkerService workerService;

    /**
     * @param requestParam
     * @return com.yunyi.response.ResponseResult
     * @Description : 员工信息申请
     * @Author : leizhang
     * @Date 2:45 下午 2021/8/17
     **/
    @PostMapping("/submitWorkerInfo")
    public ResponseResult submitWorkerInfo(@RequestBody Map<String, Object> requestParam) {
        try {
            //18:00之后不可新增订单
            if (!DateUtil.getDateBefore_18()) {
                return  new ResponseResult(UserCode.ORDER_SAVE_FAIL);
            }
            ResponseResult responseResult = null;
            int i = workerService.saveWorkInfo(requestParam);
            switch (i) {
                case Constant.NUMBER_1:
                    responseResult = new ResponseResult(CommonCode.SAVE_SUCCESS);
                    break;
                case Constant.NUMBER_3:
                    responseResult = new ResponseResult(CommonCode.QUEUE_SUCCESS);
                    break;
                default:
                    responseResult = new ResponseResult(CommonCode.HAVE_ORDER);
                    break;
            }
            return responseResult;
        } catch (Exception e) {
            LOGGER.error("员工信息申请错误:[{}]", e.getMessage(), e);
            return new ResponseResult(CommonCode.FAIL);
        }
    }

    /**
     * @param requestParam
     * @return com.yunyi.response.ResponseResult
     * @Description : 员工信息获取
     * @Author : leizhang
     * @Date 2:48 下午 2021/8/17
     **/
    @GetMapping("/getWorkerInfo")
    public QueryResponseResult getWorkerInfo(@RequestParam Map<String, Object> requestParam) {
        try {
            List<WorkTimeDto> workerInfo = workerService.getWorkerInfo(requestParam);
            QueryResult<WorkTimeDto> queryResult = new QueryResult<>();
            queryResult.setList(workerInfo);
            queryResult.setTotal(workerInfo.size());
            return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
        } catch (Exception e) {
            LOGGER.error("员工信息获取错误:[{}]", e.getMessage(), e);
            return new QueryResponseResult(CommonCode.FAIL, null);
        }
    }

    /**
     * @param
     * @return com.yunyi.response.ResponseResult
     * @Description : 员工上班签到
     * @Author : leizhang
     * @Date 7:11 下午 2021/8/17
     **/
    @PostMapping("/updateWorkTime")
    public ResponseResult updateWorkTime(@RequestBody Map<String, Object> requestParam) {
        ResponseResult responseResult = new ResponseResult(CommonCode.SUCCESS);
        try {
            workerService.updateWorkTime(requestParam);
        } catch (Exception e) {
            LOGGER.error("员工签到失败:[{}]", e.getMessage(), e);
            responseResult = new ResponseResult(CommonCode.FAIL);
            responseResult.setMessage(e.getMessage());
            return responseResult;
        }
        return responseResult;
    }

    /**
     * @param
     * @return com.yunyi.response.ResponseResult
     * @Description : 订单取消
     * @Author : leizhang
     * @Date 10:23 下午 2021/8/21
     **/
    @PostMapping("/updateWorkStatus")
    public ResponseResult updateWorkStatus(@RequestBody Map<String, Object> requestParam) {
        try {
                //18：00 取消订单不被允许
            if (!DateUtil.getDateBefore_18()) {
                return new ResponseResult(UserCode.ORDER_FAIL);
            }
            workerService.updateWorkStatus(requestParam);
            return new ResponseResult(UserCode.ORDER_SUCCESS);
        } catch (Exception e) {
            LOGGER.error("工作状态取消失败:[{}]", e.getMessage(), e);
            return new ResponseResult(UserCode.ORDER_CANCEL_FAIL);
        }
    }

    /**
     * @param
     * @return com.yunyi.response.ResponseResult
     * @Description : 获取员工订单信息
     * @Author : leizhang
     * @Date 1:28 上午 2021/8/22
     **/
    @GetMapping("/getWorkInfoByStatus")
    public QueryResponseResult getWorkInfoByStatus(@RequestParam Map<String, Object> requestParam) {
        try {
            List<WorkOrderInfo> workInfoByStatus = workerService.getWorkInfoByStatus(requestParam);
            QueryResult queryResult = new QueryResult();
            queryResult.setList(workInfoByStatus);
            queryResult.setTotal(workInfoByStatus.size());
            return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
        } catch (Exception e) {
            LOGGER.error("工作信息获取失败:[{}]", e.getMessage(), e);
            return new QueryResponseResult(CommonCode.FAIL, null);
        }
    }

    /**
     * @param requestParam {workerId:app_id}
     * @Description : 获取员工当天工作信息
     * @Author : leizhang
     * @Date 10:17 上午 2021/8/25
     **/
    @GetMapping("/getWorkerInfoOnDay")
    public QueryResponseResult getWorkerInfoOnDay(@RequestParam Map<String, Object> requestParam) {
        try {
            requestParam.put(Constant.WORK_STATUS, Constant.NUMBER_2);
            List<WorkOrderInfo> workInfoByStatus = workerService.getWorkInfoByStatus(requestParam);
            QueryResult queryResult = new QueryResult();
            queryResult.setList(workInfoByStatus);
            queryResult.setTotal(workInfoByStatus.size());
            return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
        } catch (Exception e) {
            LOGGER.error("工作信息获取失败:[{}]", e.getMessage(), e);
            return new QueryResponseResult(CommonCode.FAIL, null);
        }
    }
}
